package view;

import controller.IUnidadeCtrl;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.IUnidade;
import model.Observer;

public class UnidadeGUI extends javax.swing.JFrame implements Observer{
    
    IUnidade model;
    IUnidadeCtrl unidadeCtrl;               
             
    public UnidadeGUI(IUnidade model,IUnidadeCtrl unidadeCtrl) {
        this.unidadeCtrl = unidadeCtrl;       
        this.model = model;    
        this.model.registrarObserver(this);  
    }
     
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jtfdDescricao = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jtfdCodigo = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jbtnCancelar = new javax.swing.JButton();
        jbtnExcluir = new javax.swing.JButton();
        jbtnSalvar = new javax.swing.JButton();
        jbtnNovo = new javax.swing.JButton();
        jbtnPrimeiro = new javax.swing.JButton();
        jbtnAnterior = new javax.swing.JButton();
        jbtnProximo = new javax.swing.JButton();
        jbtnUltimo = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jcbxPesquisa = new javax.swing.JComboBox();
        jtfdPesquisa = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblPesquisa = new javax.swing.JTable();
        jbtnPesquisar = new javax.swing.JButton();
        jSeparator12 = new javax.swing.JSeparator();
        jSeparator13 = new javax.swing.JSeparator();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Controle de Unidades");
        setMinimumSize(new java.awt.Dimension(679, 396));
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel7.setBackground(new java.awt.Color(0, 0, 0));
        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 255));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Controle de Unidades");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(5, 0, 710, -1));

        jPanel1.setBackground(new java.awt.Color(217, 228, 231));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(102, 0, 255), new java.awt.Color(102, 0, 255)), "Dados da Unidade", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jtfdDescricao.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jtfdDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfdDescricaoKeyReleased(evt);
            }
        });
        jPanel1.add(jtfdDescricao, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 40, 440, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Descrição:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 20, -1, -1));

        jtfdCodigo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jtfdCodigo.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtfdCodigo.setEnabled(false);
        jPanel1.add(jtfdCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, 170, 20));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel12.setText("Código:");
        jPanel1.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 20, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 240, 680, 80));

        jPanel2.setBackground(new java.awt.Color(217, 228, 231));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 0, 255)));

        jbtnCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/can.png"))); // NOI18N
        jbtnCancelar.setMnemonic('L');
        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnExcluir.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/excluir25.png"))); // NOI18N
        jbtnExcluir.setMnemonic('I');
        jbtnExcluir.setText("Excluir");
        jbtnExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExcluirActionPerformed(evt);
            }
        });

        jbtnSalvar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/atualizar25.png"))); // NOI18N
        jbtnSalvar.setMnemonic('t');
        jbtnSalvar.setText("Salvar");
        jbtnSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSalvarActionPerformed(evt);
            }
        });

        jbtnNovo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/incl.png"))); // NOI18N
        jbtnNovo.setMnemonic('x');
        jbtnNovo.setText("Novo");
        jbtnNovo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNovoActionPerformed(evt);
            }
        });

        jbtnPrimeiro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnPrimeiro.setText("<<");
        jbtnPrimeiro.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPrimeiroActionPerformed(evt);
            }
        });

        jbtnAnterior.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnAnterior.setText("<");
        jbtnAnterior.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAnteriorActionPerformed(evt);
            }
        });

        jbtnProximo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnProximo.setText(">");
        jbtnProximo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnProximoActionPerformed(evt);
            }
        });

        jbtnUltimo.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnUltimo.setText(">>");
        jbtnUltimo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnUltimoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnNovo, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnPrimeiro, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnProximo, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnUltimo, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbtnPrimeiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnUltimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbtnSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jbtnProximo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 680, -1));

        jPanel3.setBackground(new java.awt.Color(217, 228, 231));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED, new java.awt.Color(102, 0, 255), new java.awt.Color(102, 0, 255)), "Unidades", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12), java.awt.Color.black)); // NOI18N
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jcbxPesquisa.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jcbxPesquisa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(jcbxPesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 110, 20));

        jtfdPesquisa.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jtfdPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtfdPesquisaKeyReleased(evt);
            }
        });
        jPanel3.add(jtfdPesquisa, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 40, 390, 20));

        jtblPesquisa.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jtblPesquisa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, "", null, null},
                {null, null, null, null}
            },
            new String [] {
                "Título 1", "Título 2", "Título 3", "Título 4"
            }
        ));
        jtblPesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblPesquisaMouseClicked(evt);
            }
        });
        jtblPesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtblPesquisaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jtblPesquisa);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 640, 110));

        jbtnPesquisar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnPesquisar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/pesquisar.png"))); // NOI18N
        jbtnPesquisar.setMnemonic('C');
        jbtnPesquisar.setText("Buscar");
        jbtnPesquisar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbtnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPesquisarActionPerformed(evt);
            }
        });
        jPanel3.add(jbtnPesquisar, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 30, 100, 30));

        jSeparator12.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jPanel3.add(jSeparator12, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, -1, 20));

        jSeparator13.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jPanel3.add(jSeparator13, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 40, 10, 20));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel10.setText("Pesquisar Por:");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 130, -1));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 680, 200));

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jXTable1);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 400, 690, 230));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1032)/2, (screenSize.height-764)/2, 1032, 764);
    }// </editor-fold>//GEN-END:initComponents

    private void jtblPesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblPesquisaMouseClicked
        jtfdCodigo.setText((String) jtblPesquisa.getValueAt(jtblPesquisa.getSelectedRow(), 0));
        jtfdDescricao.setText((String) jtblPesquisa.getValueAt(jtblPesquisa.getSelectedRow(), 1));                
    }//GEN-LAST:event_jtblPesquisaMouseClicked

    private void jbtnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPesquisarActionPerformed
        if ((jcbxPesquisa.getSelectedItem() == "Código") && (jtfdPesquisa.getText().equals(""))){
            JOptionPane.showMessageDialog(null, "Informe o Código!");
            jtfdCodigo.requestFocus();
        }else{            
            String iPesquisa = jtfdPesquisa.getText(); 
            int iOpcao = 0; 
            if (jcbxPesquisa.getSelectedItem() == "Código"){
                iOpcao = IUnidade.SQL_CODIGO;
            }else{
                iOpcao = IUnidade.SQL_DESCRICAO;
            }            
            atualizarDadosTable(unidadeCtrl.pesquisar(iOpcao, iPesquisa));   
        }
    }//GEN-LAST:event_jbtnPesquisarActionPerformed

    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        limpaTela();
    }//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jbtnExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExcluirActionPerformed
        String[] botoes = {"Sim", "Não"};
        int n = JOptionPane.showOptionDialog(this, "Tem certeza que deseja excluir o item?",
                "Apagar ?", JOptionPane.DEFAULT_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, botoes, botoes[0]);
        if (n == 0)  {                
            unidadeCtrl.excluir(Integer.parseInt(jtfdCodigo.getText()));            
            limpaTela();
        }
    }//GEN-LAST:event_jbtnExcluirActionPerformed

    private void jbtnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSalvarActionPerformed
        int codigoAtual = Integer.parseInt(jtfdCodigo.getText());
        String descricao = jtfdDescricao.getText();
        
        if (codigoAtual > 0) {
            unidadeCtrl.atualizar(codigoAtual,descricao);
        }else{     
            unidadeCtrl.inserir(codigoAtual,descricao);
            jtfdCodigo.setText(String.valueOf(unidadeCtrl.getCodigo()));            
        }               
        habilitaDesabilitaBotoes(true);        
    }//GEN-LAST:event_jbtnSalvarActionPerformed

    private void jbtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNovoActionPerformed
        jtfdDescricao.requestFocus();
        jtfdCodigo.setText("0");
        jtfdDescricao.setText(null);        
        habilitaDesabilitaBotoes(false);                   
    }//GEN-LAST:event_jbtnNovoActionPerformed

    private void jbtnPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPrimeiroActionPerformed
        jtblPesquisa.setRowSelectionInterval(0, 0);
        jtblPesquisaMouseClicked(null);
    }//GEN-LAST:event_jbtnPrimeiroActionPerformed

    private void jbtnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAnteriorActionPerformed
        int linhaSelecionada = jtblPesquisa.getSelectedRow();
        if (linhaSelecionada > 0){
            jtblPesquisa.setRowSelectionInterval(linhaSelecionada-1, linhaSelecionada-1);
            jtblPesquisaMouseClicked(null);
        }
    }//GEN-LAST:event_jbtnAnteriorActionPerformed

    private void jbtnProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnProximoActionPerformed
        int linhaSelecionada = jtblPesquisa.getSelectedRow();
        int totalLinhas = jtblPesquisa.getRowCount();
        
        if (linhaSelecionada < (totalLinhas - 1)){
            jtblPesquisa.setRowSelectionInterval(linhaSelecionada+1, linhaSelecionada+1);
            jtblPesquisaMouseClicked(null);
        }
    }//GEN-LAST:event_jbtnProximoActionPerformed

    private void jbtnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnUltimoActionPerformed
        int totalLinhas = jtblPesquisa.getRowCount();
        jtblPesquisa.setRowSelectionInterval(totalLinhas-1, totalLinhas-1);
        jtblPesquisaMouseClicked(null);
    }//GEN-LAST:event_jbtnUltimoActionPerformed

    private void jtblPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtblPesquisaKeyReleased
        jtblPesquisaMouseClicked(null);
    }//GEN-LAST:event_jtblPesquisaKeyReleased

    private void jtfdDescricaoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfdDescricaoKeyReleased
        if (!jbtnCancelar.isEnabled()){
            habilitaDesabilitaBotoes(false);            
        }
    }//GEN-LAST:event_jtfdDescricaoKeyReleased

    private void jtfdPesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfdPesquisaKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER){
            jbtnPesquisarActionPerformed(null);            
        }
    }//GEN-LAST:event_jtfdPesquisaKeyReleased
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator12;
    private javax.swing.JSeparator jSeparator13;
    private org.jdesktop.swingx.JXTable jXTable1;
    private javax.swing.JButton jbtnAnterior;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnExcluir;
    private javax.swing.JButton jbtnNovo;
    private javax.swing.JButton jbtnPesquisar;
    private javax.swing.JButton jbtnPrimeiro;
    private javax.swing.JButton jbtnProximo;
    private javax.swing.JButton jbtnSalvar;
    private javax.swing.JButton jbtnUltimo;
    private javax.swing.JComboBox jcbxPesquisa;
    private javax.swing.JTable jtblPesquisa;
    private javax.swing.JTextField jtfdCodigo;
    private javax.swing.JTextField jtfdDescricao;
    private javax.swing.JTextField jtfdPesquisa;
    // End of variables declaration//GEN-END:variables
        
    private void limpaTela(){
        jtfdCodigo.setText("0");
        jtfdDescricao.setText(null);        
        habilitaDesabilitaBotoes(true); 
    }    
    
    public void inserirDadosTable(ArrayList list){ 
        DefaultTableModel modelo = new DefaultTableModel(new String[]{"Código","Descrição"}, 0);                                
        jtblPesquisa.setModel(modelo);               
        for (int i = 0; i < list.size(); i++) {
            modelo.addRow((String[])list.get(i));            
        }
        jXTable1.set
    }
    
    private void atualizarDadosTable(){ 
        ArrayList list;
        list = unidadeCtrl.getAll();
        inserirDadosTable(list);
    } 
    
    private void atualizarDadosTable(ArrayList list){ 
        inserirDadosTable(list);
    }            
    
    private void inserirDadosCombo(){        
        jcbxPesquisa.setModel(new DefaultComboBoxModel(new String[]{"Código"}));            
        jcbxPesquisa.addItem("Descrição");            
    }
    
    private void habilitaDesabilitaBotoes(boolean Habilita){        
        jbtnPrimeiro.setEnabled(Habilita);
        jbtnAnterior.setEnabled(Habilita);
        jbtnProximo.setEnabled(Habilita);
        jbtnUltimo.setEnabled(Habilita);
        jbtnNovo.setEnabled(Habilita);
        jbtnSalvar.setEnabled(!Habilita);
        jbtnCancelar.setEnabled(!Habilita);
        jbtnExcluir.setEnabled(Habilita);
        jbtnPesquisar.setEnabled(Habilita);        
    }   
    
    private void icoTitle(){
        java.net.URL imgURL = UnidadeGUI.class.getResource("/icones/ico_barra.png");
        setIconImage(new ImageIcon(imgURL).getImage());
    }
    
    private void atualizarDados() {
        atualizarDadosTable();
        inserirDadosCombo();
    }
    
    public void criarTela(){
        initComponents();
        setVisible(true);
        habilitaDesabilitaBotoes(true);        
        atualizarDados();
        icoTitle();
    }

    @Override
    public void update() {
        atualizarDados();
    }  
    
}
